package com.next.androidintentlibrary;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

import ohos.app.Context;
import ohos.utils.net.Uri;

public class BrowserIntents {
    private Context context;
    private Intent intent;
    private BrowserIntents(Context context)
    {
        this.context = context;
    }
    public static BrowserIntents from(Context context)
    {
        return new BrowserIntents(context);
    }
    public BrowserIntents openBrowser()
    {
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse("about:blank"));
        intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        return this;
    }

    public BrowserIntents openLink(String url)
    {
        // if protocol isn't defined use http by default
        if (!url.isEmpty() && !url.contains("://"))
        {
            url = "http://" + url;
        }

        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse(url));
        return this;
    }

    public BrowserIntents openLink(Uri uri)
    {
        return openLink(uri.toString());
    }

    public BrowserIntents openBaidu()
    {
        Uri uri = Uri.parse("https://Baidu.com");
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(uri);
        return this;
    }



    public Intent build()
    {
        return intent;
    }

    private void startAbility(Intent intent)
    {
        if (!(context instanceof Ability))
        {
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        }
        context.startAbility(intent,0);
    }

    public void show()
    {
        startAbility(build());
    }
}
